/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import dao.DaoServico;
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import modelo.Cidade;
import modelo.Servico;
import tabeladados.TabelaDeDados;

/**
 *
 * @author fumio
 */
public class pesquisaServico extends javax.swing.JDialog {
    private TabelaDeDados tabela = null;
    private DaoServico daoservico;
    /**
     * Creates new form pesquisaServico
     */
    public pesquisaServico(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablePesquisa = new javax.swing.JTable();
        edtPesquisa = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tablePesquisa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tablePesquisa);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 100, -1, 275));

        edtPesquisa.setText("jTextField1");
        jPanel1.add(edtPesquisa, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, 430, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(pesquisaServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(pesquisaServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(pesquisaServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(pesquisaServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                pesquisaServico dialog = new pesquisaServico(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField edtPesquisa;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablePesquisa;
    // End of variables declaration//GEN-END:variables

        public final void atualizarTabela() {

        if (tabela == null) {
            tabela = new TabelaDeDados(tablePesquisa);

            tabela.adicionarColuna("Código", 15);
            tabela.adicionarColuna("Cidade", 40);
            tabela.adicionarColuna("UF", 45);
            tablePesquisa.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                @SuppressWarnings("empty-statement")
                public void valueChanged(ListSelectionEvent e) {
                    if (e.getValueIsAdjusting()) {
                       

                    }
                    ;
                }
            });

        }


        tabela.removerTodos();

        String select = "select c from Cidade c";
        if (!edtPesquisa.getText().equals("")) {
            select = select + " where c.cidade like '%" + edtPesquisa.getText() + "%'";
        }
        select = select + " order by c.cod_cidade";

        List dados = daoservico.select(select);

        Cidade aux = null;
        for (int i = 0; i < dados.size(); i++) {
            aux = (Cidade) dados.get(i);
            Object[] linha = {aux.getCod_cidade(), aux.getCidade(), aux.getUf()};
            tabela.adicionarLinha(linha);
        }
        tabela.setDados(dados);

    }
    
    public Servico Seleciona(){
        atualizarTabela();
        this.setVisible(true);
        return (Servico) tabela.getObjetoSelecionado();
    }

}
